/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package api.purchase;

import Model.Purchase.ApiPurchaseListView;
import Model.Purchase.PurchaseListView;
import Model.Purchase.PurchaseOfferList;
import Model.User.UserList;
import RssEasy.Core.HttpRequestHelper;
import RssEasy.Core.ServletRequestHelper;
import RssEasy.Core.SignVerifyRequestHelper;
import com.google.gson.JsonObject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author zhangwenbo
 */
@WebServlet(name = "api_purchase_list", urlPatterns = {"/api/purchase/list"})
public class list extends ServletRequestHelper {

    @Override
    protected HttpRequestHelper request(HttpServletRequest request, HttpServletResponse response) {
        super.request(request, response);
        try {
            ApiPurchaseListView purchase = new ApiPurchaseListView(this.pageContext);
            purchase.request();
            purchase.query("SELECT id,name,corpname,enddt,shijian,business_class,state,result,linkname,phone,enterprise FROM api_purchase_list_view WHERE myid=" + this.get("myid") + " and typid=1 GROUP BY id");
            UserList ul = new UserList(this.pageContext);
            ul.select("mainbusiness").where("enterprise=?", this.get("myid")).get_first_rows();
            String mainbusiness = ul.get("mainbusiness");
            String[] main = mainbusiness.split(",");
            String conditions = "";
            if (main.length == 1) {
                conditions = "business_class like '%" + mainbusiness + "%'";
            } else {
                for (int n = 0; n < main.length; n++) {
                    conditions += "business_class like '%" + main[n] + "%' or ";
                }
                conditions = conditions.substring(0, conditions.length() - 3);
            }
            ApiPurchaseListView aplv = new ApiPurchaseListView(this.pageContext);
            aplv.request();
            aplv.query("SELECT id,name,corpname,enddt,shijian,business_class,state,linkname,phone,enterprise FROM api_purchase_list_view WHERE id not IN (SELECT purchaseid FROM api_purchase_list_view WHERE myid=" + this.get("myid") + ") and typid=1 and (" + conditions + ") GROUP BY id");
            out.print("{\"yes\":" + purchase.toKeyValue() + ",\"not\":" + aplv.toKeyValue() + "}");
        } catch (Exception e) {
            out.print("{\"rsscode\":" + e.getMessage() + "}");
        }
        return this;
    }
}


